package com.btex.app;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;
import com.btex.app.adapter.MyCoinsAdapter;
import com.btex.app.entity.MyCoinsInfo;
import com.btex.app.entity.UserInfo;
import com.btex.app.myView.XListView;
import com.btex.app.tools.CookieTool;
import com.btex.app.tools.LogUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wcx on 16/3/24.
 */
public class MyCoinsActivity extends BaseActivity implements XListView.IXListViewListener, AdapterView.OnItemClickListener {
    private static final String TAG = "MyCoinsActivity";
    private XListView mListView;
    private MyCoinsAdapter myCoinsAdapter;
    private UserInfo userInfo;
    private PreferencesCookieStore cookieStore;
    private List<MyCoinsInfo>  myCoinsInfos = new ArrayList<>();
    private TextView tv_total_money;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_refresh);
        View headerView = View.inflate(cxt,R.layout.activity_header_my_account,null);
        myCoinsAdapter = new MyCoinsAdapter(cxt);
        setXListView(headerView);
        userInfo = (UserInfo) getIntent().getSerializableExtra("userInfo");
        tv_total_money = (TextView)findViewById(R.id.tv_total_money);
        tv_total_money.setText(userInfo.getTotal_fund());
        cookieStore = CookieTool.getCookieStore(cxt,sp);

        initData();
    }

    @Override
    public void setData() {
        tv_title.setText(getString(R.string.my_money));
        toLoadMyCoins();

    }

    private void toLoadMyCoins() {

        OkGo.<String>get(Config.URL_MY_COINS).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String result = response.body();
                LogUtil.i(TAG, "result" + result);
                try {
                    JSONObject json = new JSONObject(result);
                    if ("1".equals(json.getString(Config.KEY_CODE))) {
                        Type type = new TypeToken<ArrayList<MyCoinsInfo>>() {
                        }.getType();
                        myCoinsInfos = new Gson().fromJson(json.getJSONObject(Config.KEY_DATA).getString("coins"), type);
                        myCoinsAdapter.addData(true, myCoinsInfos);
                    }
                } catch (JSONException e) {
                    LogUtil.e(TAG, e.toString());
                }
                stopRefesh();
            }
        });

//        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.GET, Config.URL_MY_COINS, new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//                String result = responseInfo.result.toString();
//                LogUtil.i(TAG, "result" + result);
//                try {
//                    JSONObject json = new JSONObject(result);
//                    if ("1".equals(json.getString(Config.KEY_CODE))) {
//                        Type type = new TypeToken<ArrayList<MyCoinsInfo>>() {
//                        }.getType();
//                        myCoinsInfos = new Gson().fromJson(json.getJSONObject(Config.KEY_DATA).getString("coins"), type);
//                        LogUtil.i(TAG, "myCoinsInfos" + myCoinsInfos.toString());
//                        myCoinsAdapter.addData(true, myCoinsInfos);
//                    }
//                } catch (JSONException e) {
//                    LogUtil.e(TAG, e.toString());
//                }
//                stopRefesh();
//            }
//
//            @Override
//            public void onFailure(HttpException error, String msg) {
//                stopRefesh();
//            }
//        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
        }
    }

    /**
     * 初始化Listview
     * @param headerView
     */
    private void setXListView(View headerView) {
        mListView = (XListView)findViewById(R.id.list);
        mListView.setXListViewListener(this);
        mListView.setOnItemClickListener(this);
        mListView.setPullLoadEnable(false);
        mListView.setPullRefreshEnable(true);
        mListView.addHeaderView(headerView);
        mListView.setAdapter(myCoinsAdapter);
    }

    @Override
    public void onRefresh() {
        toLoadMyCoins();
    }

    @Override
    public void onLoadMore() {

    }

    public void stopRefesh(){
        mListView.stopLoadMore();
        mListView.stopRefresh();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(position>1){
            Intent i = new Intent(cxt,TradeActivity.class);
            MyCoinsInfo myCoinsInfo = (MyCoinsInfo) myCoinsAdapter.getItem(position-2);
            i.putExtra("pair",myCoinsInfo.getType()+"_CNY");
            startActivity(i);
        }

    }


}